/**
 * Outcast
 */

public class Outcast {

    private WordNet wordnet;

    // constructor takes a WordNet object
    public Outcast(WordNet wordnet) {
        this.wordnet = wordnet;
    }

    // given an array of WordNet nouns, return an outcast
    public String outcast(String[] nouns) {
        int[] dist = new int[nouns.length];

        int i = 0;
        while (i < nouns.length) {
            int distForNoun = 0;
            int j = 0;
            while (j < nouns.length) {
                int d = wordnet.distance(nouns[i], nouns[j]);
                distForNoun += d;
                j++;
            }
            dist[i] = distForNoun;
            i++;
        }

        //get the max in dist[]
        int max = max(dist);
        return nouns[max];
    }

    private int max(int[] i) {
        int max = i[0];
        int maxIdx = 0;
        for (int j = 0; j < i.length; j++) {
            int anI = i[j];
            if (anI > max) {
                max = anI;
                maxIdx = j;
            }
        }
        return maxIdx;
    }


    public static void main(String[] args) {
        WordNet wordnet = new WordNet(args[0], args[1]);
        Outcast outcast = new Outcast(wordnet);
        for (int t = 2; t < args.length; t++) {
            String[] nouns = In.readStrings(args[t]);
            StdOut.println(args[t] + ": " + outcast.outcast(nouns));
        }
    }

}